<?php
class mpointClass extends  basicClass {
	
	public $objDb;
	public $Block;
	public $objBase;
	public $objSortList;
	public $check;

	
 	function __construct(){
 		
        require_once(dirname(__FILE__)."/../config.inc.php");
		//include_once CFG_LIB_DIR.'global.func.php';
		
		include_once(CFG_LIB_DIR.'mysqldb.inc.php');
		$this->objDb = new mysqldb();
		
		include_once(CFG_LIB_DIR."block.php");
		$this->Block = new clsBlock();
		
		include_once(CFG_LIB_DIR."generic.lib.php");
		$this->objBase = new clsBase();
		
		include_once(CFG_LIB_DIR."category.php");
		$this->objSortList = new clsCategory();
		
		require CFG_LIB_DIR."check.php";
		$this->check = new check_ifno();
    }
	
    
    /**
     * 充值
     *
     */
    function chongZhi(){
    	$sql = " select *  FROM  hy_b_card WHERE account = '".$_POST['kahao']."' AND psw = '".$_POST['password']."' AND state = 0 ";
    	$this->objDb->query($sql);
    	$result = $this->objDb->get_data();
    	
    	if(empty($result)){ 	
    		$this->objBase -> popup('没有该 卡或卡号密码输入有误或卡已被充值！');
    	} else {
    		//计算销售金额
    		$sql = " select jieshaorenid from hy_member where user_id = ".$_SESSION['user_id'];
    	    $this->objDb->query($sql);
    	    $jsr = $this->objDb->get_data();
    		
    	    if(!empty($jsr)){
    	    	$sql = "  update hy_member set `salemoney`=`salemoney`+".$result[0]['money']." where user_id = ".$jsr[0]['jieshaorenid'];
    	    	$this->objDb->query($sql);
    	    }
			
			//加积分 
    		$sql = "  update hy_member set `money` =`money`+".$result[0]['money'].",`zhifen`=`zhifen`+".$result[0]['zhifen']."  where user_id = ".$_SESSION['user_id'];
    		$this->objDb->query($sql);
    	    
    	    //改变充值卡状态 
    	    $sql = "  update hy_b_card set `state` = 1  WHERE account = '".$_POST['kahao']."' AND psw = '".$_POST['password']."'";
    	    $this->objDb->query($sql);
    		
    	    //处理纪录 
    		$content = date("Y年m月d日Y:h:i",time()).",".$_SESSION['user_name'].'充值'.$result[0]['money'].'万元的套餐，获得'.$result[0]['money'].'点积分';
    		$arr=array(   
    		            "name"       => $membername,
					    "content"    => $content,
					    "type"       => 1,
					    "createtime" => time(),
					);
			
		    $this->saveLog($arr);

		    $str = '您好！'.$_SESSION['user_name'].'，您已充值成功，获得'.$result[0]['zhifen'].'点积分！【M－point】';

		   
 			
			$str = iconv('UTF-8', 'GB2312', $str);
		    
		    //发送短信
			$ch = curl_init() or die (curl_error());
			 //设置URL参数
			$href = "http://118.126.11.49:8080/Message.sv?method=sendMsg&userCode=jice&userPwd=jjcc009&numbers=".$_SESSION['user_tel']."&msgContent=".$str."&charset=gbk";
			
			curl_setopt($ch,CURLOPT_URL,$href);
			 //要求CURL返回数据
			curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
			 //执行请求
			$result = curl_exec($ch) or die (curl_error());
			
			 //取得返回的结果，并显示
			 //$result;
			 curl_error($ch);
			 //关闭CURL
			 curl_close($ch);
		    
		    $str = iconv('GB2312','UTF-8',  $str);
		    $this->objBase -> popup($str,"myzhifen.php");
 			
 			
 			exit();   		
    	}
    	
    	
    	
    }
    
    /**
     * 保存日志
     *
     * @param unknown_type $arr
     */
    function saveLog($arr){
  
		/*$arr=array(
		   "name"=>,
		    "content" = >'',
		    "type"=> ,
		    "createtime" => time(),
		);*/
		
		//$arr = $this->unsetArray($arr,$delarr); 
	    $SQL = $this-> insertData("hy_b_log",$arr);
	    $result =$this->objDb->query($SQL);
		//if($result) $this->objBase->popup("发表成功！");
		//else $this->objBase->popup("发表失败，请稍候再发！");
    }
   
	/**
	 * 转积分 
	 *
	 */
    function zJF(){
        if(empty($_POST['name'])){
    		$this->objBase->popup("转入卡号不能为空！");
    		die;
    	}elseif(empty($_POST['zhifen'])){
    		$this->objBase->popup("积分金额不能为空！");
    		die;
    	}
    	
    	echo $sql = " select `zhifen` FROM  hy_member WHERE user_id = ".$_POST['uid'];
    	$this->objDb->query($sql);
    	$result = $this->objDb->get_data();
    	if(!empty($result)){
    		if($result[0]['zhifen'] < $_POST['zhifen']) {
    			$this->objBase->popup("对不起，你的余额不够，请充值！");
    		} else {
    			$sql = " UPDATE hy_member set `zhifen` = `zhifen`-".$_POST['zhifen']." WHERE user_id = ".$_POST['uid'];
		   		$content = date("Y年m月d日Y:h:i",time()).",".$_POST['name'].'把'.$_POST['zhifen'].'点积分转入“'.$_POST['kahao'].'”用户；';
		   		$arr = array(   
		   		            "name"       => $_POST['name'],
						    "content"    => $content,
						    "type"       => 1,
						    "createtime" => time(),
						);
				
			    $this->saveLog($arr);
		    	$result =$this->objDb->query($SQL);
				if($result) $this->objBase->popup("转移成功！");
				else $this->objBase->popup("转移失败，请稍候再转！");
    		}
    	}
    	
    }    
	
}





class basicClass{
	 
	/***
	 * 插入数据
	 * @param String $table
	 * @param Array $arr
	 * @return String $sq
	 */
    public function insertData($table,$arr){
		if(is_array($arr)){
			foreach($arr as $key=>$value){
				$keys[]  = "`".$key."`";
				$vlues[] = "'".$value."'";
			}
		}

		$keystr   = implode(",", $keys);
		$valuestr = implode(",", $vlues);
		$sql      = " INSERT INTO `".$table."` (".$keystr.") VALUES(".$valuestr.")";
		return $sql;
	}
	
    /**
 	 * 
 	 * 删除不需要的数组
 	 * @param Array $array
 	 * @param Array $delarr
 	 */
	public function unsetArray($array,$delarr){
		foreach ($array as $bkey=>$bvalue){
			foreach ($delarr as $key=>$value){
				if($value == $bkey){
				unset($array[$bkey]);		
			    }
		     }
	    }
		return $array;
	}
	
	/**
	 * 修改多字段资料
	 *
	 * @param unknown_type $table
	 * @param unknown_type $arr
	 * @param unknown_type $uid
	 * @return unknown
	 */
	public function updateArr($table,$arr,$where){
		foreach ($arr as $key=>$value){
		   $newarr[] = "`".$key."` = '".$value."'";
	    }
	    foreach ($where as $key=>$value){
		   $wherearr[] = "`".$key."` = '".$value."'";
	    }
	    $strs  = implode(",",$newarr);
	    $where = implode(" AND ",$wherearr);
	    $sql = " UPDATE ".$table." SET ".$strs ." WHERE 1 AND ".$where;
		return $sql;
	}
	
	
}

//$bbs = new BBSClass();
//$bbs->doLogin();
//echo $bbs->insertData("NEWS",array("Title"=>"2323",'CONTENT'=>2323));